//
//  RootViewController.m
//  UI12_Block传值
//
//  Created by TravelRound on 15/11/24.
//  Copyright © 2015年 TravelRound. All rights reserved.
//

#import "RootViewController.h"
#import "SecondViewController.h"

@interface RootViewController ()


@property (nonatomic, retain) UILabel *label;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(action)] autorelease];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    self.label.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.label];
    [_label release];
    
    
    
    
//    void (^block1)(void) = ^(void) {
//        
//        NSLog(@"aaa");
//        
//    };
//    block1();
//    
//    void (^block2)(NSString *) = ^(NSString *str) {
//        
//        NSLog(@"%@", str);
//        
//    };
//    block2(@"bbb");
    
}

- (void)action
{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    
//    __unsafe_unretained RootViewController *rootVC = self;
//    
//    void (^colorBlock)(UIColor *) = ^(UIColor *myColor) {
//      
//        rootVC.view.backgroundColor = myColor;
//        
//    };
//    
//    [secondVC sendBlock:colorBlock str:@"aaa"];
    
#warning block传值1 - 定义block, 并通过调用secondVC的方法将block地址传递过去
    __block RootViewController *rootVC = self;
    
    NSLog(@"===%ld %ld %ld", self.retainCount, self.view.retainCount, self.label.retainCount);
    
    [secondVC sendBlock:^(UIColor *myColor) {
        
        rootVC.view.backgroundColor = myColor;
//        rootVC.view.backgroundColor = myColor;
        rootVC.label.text = @"aaa";
        
    } str:@"aaa"];

    NSLog(@"===%ld %ld %ld", self.retainCount, self.view.retainCount, self.label.retainCount);
    
    [self.navigationController pushViewController:secondVC animated:YES];
    
    [secondVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
